package cn.haiway.drools.web;

import cn.haiway.drools.pojo.Rule;
import cn.haiway.drools.service.RuleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.net.URISyntaxException;
import java.util.Date;

@RestController
@RequestMapping("/api/rules")
@Slf4j
public class RuleController {

    @Autowired
    private RuleService ruleService;

    @PostMapping
    public ResponseEntity<Rule> createRule(@Valid @RequestBody Rule ruleDTO) throws URISyntaxException {
        log.debug("REST request to save Rule : {}", ruleDTO);
        ruleDTO.setCreateTime(new Date());
        ruleDTO.setCreateBy("System");
        Rule result = ruleService.save(ruleDTO);
        return ResponseEntity.ok().body(result);
    }

}
